package ge.android.gwpqrreader;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	IntentIntegrator integrator;
	private Button qrBtn;
	EditText content; 
	EditText format;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		qrBtn = (Button) findViewById(R.id.button1);
		qrBtn.setOnClickListener(this);
		
		content = (EditText) findViewById(R.id.editText1);
		format = (EditText) findViewById(R.id.editText2);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
//		integrator = new IntentIntegrator(this);
//		integrator.initiateScan();
				Intent intent = new Intent("com.google.zxing.client.android.SCAN");
				intent.setPackage("com.google.zxing.client.android");
				intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
				startActivityForResult(intent, 0);
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);

//		IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
		if (requestCode == 0){
			if(resultCode == RESULT_OK){
				String contents = intent.getStringExtra("SCAN_RESULT");
				String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
//				
//				Intent intent2 = new Intent(this, MenuCodeSuccess.class);
//				intent2.putExtra("qrDetails", contents);
//				startActivity(intent2);
				
				this.content.setText(contents);
				this.format.setText(format);
			}else if (resultCode == RESULT_CANCELED){
				Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400);
                toast.show();
			}
		}
	}
}
